<template>
  <el-dialog v-model="dialogFormVisible" width="30%">
    <el-form :model="form">
      <el-form-item label="工号">
        <el-input v-model="form.job_num" autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>

      <el-form-item label="性别">
        <el-radio-group v-model="form.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="职位">
        <el-input v-model="form.position" autocomplete="off" />
      </el-form-item>
      <el-form-item label="是否离职">
        <el-radio-group v-model="form.quit">
          <el-radio label="在职">在职</el-radio>
          <el-radio label="已离职">已离职</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <!--按钮-->
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="btn_ok">确定</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">
          取消
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {reactive, toRefs, getCurrentInstance} from "vue";
export default {
  emits: ["launch"],
  setup(props, {emit}) {
    const {proxy} = getCurrentInstance();
    const form = {
      job_num: "", //工号
      name: "", //名字
      sex: "", //性别
      position: "", //职位
      quit: "", //是否离职
    };
    const state = reactive({form, dialogFormVisible: false});
    function isshow() {
      state.dialogFormVisible = true;
    }
    async function addEmployes() {
      try {
        let flag = false;
        if (state.form.quit == "在职") {
          flag = true;
        } else {
          flag = false;
        }
        const obj = {
          name: state.form.name,
          job_number: state.form.job_num,
          sex: state.form.sex,
          position: state.form.position,
          workerstate: flag,
        };

        const res = await new proxy.$request(
          proxy.$urls.m().Addemployes,
          obj
        ).modePost();
        console.log(res);
        if (res.status != 200) {
          state.dialogFormVisible = false;
          new proxy.$tips(res.data.message, "warning").mess_age();
        } else {
          new proxy.$tips(res.data.message).mess_age();

          //调用父组件方法
          
          emit("launch");
          state.dialogFormVisible = false;
        }
      } catch (e) {
        new proxy.$tips(e, "error").mess_age();
        state.dialogFormVisible = false;
        new proxy.$router.push({name: "employee"});
      }
    }
    function btn_ok() {
      addEmployes();
    }

    return {...toRefs(state), isshow, btn_ok};
  },
};
</script>

<style></style>
